package com.ycsj.goldmedalnewconcept.teacher.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ycsj.common.utils.DateUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.student.activity.StudentAnalyzeActivity;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherTKAssignHomeworkActivity;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherTkMainActivity;
import com.ycsj.goldmedalnewconcept.teacher.bean.QuestionBankInfo;
import com.ycsj.goldmedalnewconcept.view.ArcView;

import java.util.List;

/**
 * 题库练习适配器
 * Created by Administrator on 2019/2/18 0018.
 */

public class QuestionBankAdapter extends BaseQuickAdapter<QuestionBankInfo.DataBean, BaseViewHolder> {
    private Context context;
    private List<QuestionBankInfo.DataBean> datas;

    public QuestionBankAdapter(Context context, int layoutResId, @Nullable List<QuestionBankInfo.DataBean> data) {
        super(layoutResId, data);
        this.context = context;
        this.datas = data;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final QuestionBankInfo.DataBean item) {

        //当前条目的推送状态,1是已经推送,0是未推送
        final String state = item.getPush_state();

        //获取当前位置
        int currPostion = helper.getLayoutPosition();

        //获取推送练习的时间
        final String push_time = item.getPush_time();

        //星星的标记
        ImageView lvStar = helper.getView(R.id.iv_star);

        //年份
        TextView tvYear = helper.getView(R.id.tv_year);

        //月份
        TextView tvMonth = helper.getView(R.id.tv_month);

        tvYear.setText(DateUtils.getYear(push_time) + "年");

        tvMonth.setText(DateUtils.getMonth(push_time) + "月");


        //星星,年份和月份的显示规则,条目的第一个位置默认全部显示,从第二个位置开始,如果年份和月份同时跟上一个条目的年份和月份相同,则这3个控件都设置为inVisable
        if (currPostion == 0) {
            //第一个位置全部显示
            lvStar.setVisibility(View.VISIBLE);
            tvYear.setVisibility(View.VISIBLE);
            tvMonth.setVisibility(View.VISIBLE);
        } else {
            //不是第一个位置
            //获取当前条目的年月
            String currTime = DateUtils.getYearAndMonth(push_time);

            //获取上一个条目的年月
            String lastTime = DateUtils.getYearAndMonth(getData().get(currPostion - 1).getCreate_time());
            if (currTime.equals(lastTime)) {
                lvStar.setVisibility(View.INVISIBLE);
                tvYear.setVisibility(View.INVISIBLE);
                tvMonth.setVisibility(View.INVISIBLE);
            } else {
                lvStar.setVisibility(View.VISIBLE);
                tvYear.setVisibility(View.VISIBLE);
                tvMonth.setVisibility(View.VISIBLE);
            }
        }

        //右边的整体布局
        LinearLayout ll_right = helper.getView(R.id.ll_right);

        //单元号
        TextView tvUnit = helper.getView(R.id.tv_unit);
        tvUnit.setText("U" + item.getUnit());

        //正在编辑题目中...,默认不显示
        TextView tvEdit = helper.getView(R.id.tv_editing);

        //布置练习的时间,设置时间时前面需要加上:布置练习时间:
        TextView tvTime = helper.getView(R.id.tv_time);

        //学生分析按钮,有三种显示状态,学生分析高亮可点击背景tv_xsff_select,学生分析灰色不可点击tv_xsff_unselect,继续编辑高亮可点击tv_jxbj
        ImageView lvSsfx = helper.getView(R.id.lv_xsfx);

        //习题分析,有两种状态,习题分析高亮可点击背景tv_xtfx_green,习题分析灰色不可点击tv_xtff_gray
        ImageView lvXtfx = helper.getView(R.id.lv_xtfx);

        //包含ArcView的相对布局
        RelativeLayout rl_out = helper.getView(R.id.rl_out);
        //显示进度和参与人数的自定义控件,调用控件的setArcViewData(int max, int num)方法,传入总人数和参与人数即可
        ArcView arcView = helper.getView(R.id.av_pb);
        arcView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onArcViewClickListener != null) {
                    onArcViewClickListener.onClick(item);
                }
            }
        });


        //获取参与测试的组员
        List<String> task_stu = item.getTask_stu();

        //获取没有参加测试的组员
        List<String> noTask_stu = item.getNotask_stu();

        int max = task_stu.size() + noTask_stu.size();

        if ("0".equals(state)) {
            //未发布练习
            tvTime.setVisibility(View.INVISIBLE);
            tvEdit.setVisibility(View.VISIBLE);
            tvEdit.setText("正在编辑题目中....");

            //右边的整体背变为ll_qb_right_bg
            ll_right.setBackgroundResource(R.drawable.ll_qb_right_unpush);

            //单元背景
            tvUnit.setBackgroundResource(R.drawable.tv_unit_unpush);

            //推送人数显示
            rl_out.setVisibility(View.INVISIBLE);

            //未发布状态下学生分析变为继续编辑按钮状态可点击
            lvSsfx.setImageResource(R.drawable.tv_jxbj);
            lvSsfx.setEnabled(true);

        } else if ("1".equals(state) || "2".equals(state) || "3".equals(state)) {
            //已发布练习
            tvTime.setVisibility(View.VISIBLE);
            tvEdit.setVisibility(View.INVISIBLE);

            if (!TextUtils.isEmpty(DateUtils.getMonthAndDayAndHourAndMinter(push_time))) {
                tvTime.setText("布置练习时间:" + DateUtils.getMonthAndDayAndHourAndMinter(push_time));
            }

            tvEdit.setText("");

            //右边的整体背变为ll_qb_right_bg
            ll_right.setBackgroundResource(R.drawable.ll_qb_right_push);

            //单元背景
            tvUnit.setBackgroundResource(R.drawable.tv_unit_push);

            //推送人数显示
            rl_out.setVisibility(View.VISIBLE);
            arcView.setArcViewData(max, task_stu.size());

            //已布置状态下学生分析按钮,比较系统当前事件和发布时间,如果当前时间>推送时间,
            if (DateUtils.getTimeSize(push_time)) {
                //布置已推送
                lvSsfx.setImageResource(R.drawable.tv_xsff_select);
                lvSsfx.setEnabled(true);
            } else {
                //布置未推送
                lvSsfx.setImageResource(R.drawable.tv_xsfx_unselect);
                lvSsfx.setEnabled(false);
            }

        }

        //学生分析,继续编辑的点击事件
        lvSsfx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("0".equals(state)) {

                    //未布置草稿,教师端-题库-布置练习
                    Intent intent = new Intent(context, TeacherTKAssignHomeworkActivity.class);
                    intent.putExtra("unit", Integer.parseInt(item.getUnit()));
                    context.startActivity(intent);
                } else if ("1".equals(state) || "2".equals(state) || "3".equals(state)) {
                    //已布置,发布状态自己比对
                    if (DateUtils.getTimeSize(push_time)) {
                        //已布置已推送
                        String total = item.getTotal();
                        String unit = item.getUnit();
                        int postion = helper.getLayoutPosition();
                        StudentAnalyzeActivity.startActivity(context, AccountUtils.getTeacherClass(context), total, unit, postion);
                    }
                }

            }
        });

        //习题分析的点击事件
        lvXtfx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //老师端查看习题分析
                //获取是否有学生作答,根据size来判断
                List<String> taskStu = item.getTask_stu();
                if (null == taskStu || taskStu.size() == 0) {
                    //老师查看题和解析-没人做(全校正确率和小题正确率都不显示)
                    TeacherTkMainActivity.goActivity(context, item.getExam_id(), 3 + "", item.getUnit(), item.getTotal());
                } else {
                    //老师查看题和解析-有人做(全校正确率和小题正确率都显示)
                    TeacherTkMainActivity.goActivity(context, item.getExam_id(), 2 + "", item.getUnit(), item.getTotal());
                }
            }
        });

    }


    /**
     * 显示已做和未做的图形控件点击事件
     */
    private OnArcViewClickListener onArcViewClickListener;

    public void setOnArcViewClickListener(OnArcViewClickListener onArcViewClickListener) {
        this.onArcViewClickListener = onArcViewClickListener;
    }

    public interface OnArcViewClickListener {
        void onClick(QuestionBankInfo.DataBean item);
    }

}
